﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController 
{
    public class SubwebsiteController : ControllerBase
    {
        //单利对象
        private static SubwebsiteController _jobsControleer;

        //实例化单列对象
        public static SubwebsiteController getController()
        {
            if (_jobsControleer == null)
            {
                _jobsControleer = new SubwebsiteController();
            }
            return _jobsControleer;
        }

        /// <summary>
        /// 数据库访问层
        /// </summary>
        private HFModel.DAL.SubwebsiteDAL dal = new HFModel.DAL.SubwebsiteDAL();

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="CityID"></param>
        /// <param name="CityName"></param>
        public void doAddSubweb(int CityID ,string CityName , string url)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            HFModel.VO.SubwebsiteModel model = new HFModel.VO.SubwebsiteModel();

            model.CityID = CityID;
            model.CityName = CityName;
            model.Url = url;
            try
            {
                model.ID = dal.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='SubwebsiteList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch(Exception Err)
            {
                result.isTrue = false;
                result.Message = Err.Message;
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到单条记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public HFModel.VO.SubwebsiteModel getModel(int ID)
        {
            HFModel.VO.SubwebsiteModel model = new HFModel.VO.SubwebsiteModel();
            int total;

            List<HFModel.VO.SubwebsiteModel> list = dal.GetSourceList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改分站
        /// </summary>
        /// <param name="CityID"></param>
        /// <param name="CityName"></param>
        /// <param name="ID"></param>
        public void doModifySubweb(int CityID, string CityName, int ID ,string url)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            HFModel.VO.SubwebsiteModel model = this.getModel(ID);

            model.CityID = CityID;
            model.CityName = CityName;
            model.Url = url;
            try
            {
                dal.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='SubwebsiteList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 删除分站
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeSubweb(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                dal.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='SubwebsiteList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<HFModel.VO.SubwebsiteModel> getList(out string PagerStr)
        {
            List<HFModel.VO.SubwebsiteModel> list = new List<HFModel.VO.SubwebsiteModel>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = dal.GetSourceList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }
    }
}
